﻿using LightingPlatform.TCP;
using LightingPlatform.Tools;
using LightingPlatform.ViewModel;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Browser;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace LightingPlatform.View.Level4
{
    public partial class LightingVersionView : UserControl, IPageProvider, IExportData
    {
        public LightingVersionView()
        {
            InitializeComponent();

            for (int i = 0; i < 21; i++)
            {
                Grid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1, GridUnitType.Star) });
            }

            Pictures.SelectionChanged += Pictures_SelectionChanged;

            Loading();
            LoadingRelateProduct();
        }

        public async void ExportToExcel()
        {
            busy.IsBusy = true;
            busy.BusyContent = "正在导出数据，请稍等......";
            byte[] response = await TCPClient.Current.Call((int)CommandType.QueryLevel4ExportExcel, Encoding.UTF8.GetBytes(LightingContext.Singleton.QueryParameter4.ID));
            busy.IsBusy = false;
            if (response == null || response.Length == 0 || response[0] == 0)
            {
                MessageHelper.ShowError("加载数据出错。");
                return;
            }

            string url = Encoding.UTF8.GetString(response, 0, response.Length);
            HtmlPage.Window.Navigate(new Uri(url));
        }

        public async void ExportToPdf()
        {
            busy.IsBusy = true;
            busy.BusyContent = "正在导出数据，请稍等......";
            byte[] response = await TCPClient.Current.Call((int)CommandType.QueryLevel4ExportPdf, Encoding.UTF8.GetBytes(LightingContext.Singleton.QueryParameter4.ID));
            busy.IsBusy = false;
            if (response == null || response.Length == 0 || response[0] == 0)
            {
                MessageHelper.ShowError("加载数据出错。");
                return;
            }

            string url = Encoding.UTF8.GetString(response, 0, response.Length);
            HtmlPage.Window.Navigate(new Uri(url));
        }

        private void Pictures_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            LightingViewModel lighting = (LightingViewModel)Pictures.SelectedItem;
            if (lighting == null)
                return;

            QueryParameter3.Builder builder = new QueryParameter3.Builder();
            if (lighting.Place > 0)
                builder.SetUsePlace(lighting.Place);

            if (lighting.Project != null && lighting.Project.Setup > 0)
                builder.SetSetup(lighting.Project.Setup);

            if (lighting.Class > 0)
                builder.SetProductClass(lighting.Class);

            if (!string.IsNullOrEmpty(lighting.Lable))
                builder.SetLable(lighting.Lable);

            if (!string.IsNullOrEmpty(lighting.Series))
                builder.SetSeries(lighting.Series);

            if (lighting.AdapterLighting != null && lighting.AdapterLighting.Class > 0)
                builder.SetAdpaterClass(lighting.AdapterLighting.Class);

            LightingContext.Singleton.QueryParameter3 = builder.Build();
            LightingContext.Singleton.MainPageNavigated = false;
            LightingContext.Singleton.Navigation.Navigate(new Uri("/View/Level3/LightingListView.xaml", UriKind.RelativeOrAbsolute));
        }

        private LightingViewModel ConvertForm(Lighting lighting)
        {
            LightingViewModel vm = new LightingViewModel();
            vm.ID = lighting.Id;
            if (lighting.HasSeries)
            {
                vm.Series = lighting.Series;
            }
            if (lighting.HasClass)
            {
                vm.Class = lighting.Class;
            }
            if (lighting.HasVersion)
            {
                vm.Version = lighting.Version;
            }
            if (lighting.HasLable)
            {
                vm.Lable = lighting.Lable;
            }
            if (lighting.HasPlace)
            {
                vm.Place = lighting.Place;
            }

            if (lighting.HasPhysical)
            {
                vm.Physical = new PhysicalVM();
                if (lighting.Physical.HasColor)
                {
                    vm.Physical.Color = lighting.Physical.Color;
                }
                if (lighting.Physical.HasSize)
                {
                    vm.Physical.Size = lighting.Physical.Size;
                }
                if (lighting.Physical.HasDefend)
                {
                    vm.Physical.Defend = lighting.Physical.Defend;
                }
                if (lighting.Physical.HasImpact)
                {
                    vm.Physical.Impact = lighting.Physical.Impact;
                }
            }

            if (lighting.HasAdapterLighting)
            {
                vm.AdapterLighting = new AdapterLightingVM();

                if (lighting.AdapterLighting.HasClass)
                {
                    vm.AdapterLighting.Class = lighting.AdapterLighting.Class;
                }
                if (lighting.AdapterLighting.HasColor)
                {
                    vm.AdapterLighting.Color = lighting.AdapterLighting.Color;
                }
                if (lighting.AdapterLighting.HasCri)
                {
                    vm.AdapterLighting.Cri = lighting.AdapterLighting.Cri.ToString();
                }
            }


            if (lighting.HasElectric && lighting.Electric.HasLightingPower)
            {
                vm.Electric = new ElectricVM();
                vm.Electric.LightingPower = lighting.Electric.LightingPower;
            }

            if (lighting.HasOptics && lighting.Optics.HasAngle)
            {
                vm.Optics = new OpticsVM();
                vm.Optics.Angle = lighting.Optics.Angle.ToString();
            }

            if (lighting.HasBusiness && lighting.Business.HasMarket)
            {
                vm.Business = new BusinessVM();
                vm.Business.Market = lighting.Business.Market.ToString();
            }

            if (lighting.HasPicture)
            {
                vm.Picture = new PictureVM();

                if (lighting.Picture.HasFacade)
                    vm.Picture.FacadeView = lighting.Picture.Facade;
                if (lighting.Picture.HasLive)
                    vm.Picture.LiveView = lighting.Picture.Live;
                if (lighting.Picture.HasSize)
                    vm.Picture.SizeView = lighting.Picture.Size;
                if (lighting.Picture.HasGrading1)
                    vm.Picture.Grading1View = lighting.Picture.Grading1;
            }

            return vm;
        }

        private async void Loading()
        {
            byte[] response = await TCPClient.Current.Call((int)CommandType.QueryLevel4, Encoding.UTF8.GetBytes(LightingContext.Singleton.QueryParameter4.ID));
            if (response == null || response.Length == 0 || response[0] == 0)
            {
                MessageHelper.ShowError("加载数据出错。");
                return;
            }
            Lighting lighting = Lighting.ParseFrom(response);
            this.DataContext = ConvertForm(lighting);
        }

        private async void LoadingRelateProduct()
        {
            RelatedProduct.Builder builder = new RelatedProduct.Builder();
            LightingViewModel ligting = LightingContext.Singleton.QueryParameter4;
            if (ligting.Place > 0)
                builder.SetUsePlace(ligting.Place);

            if (ligting.Project != null && ligting.Project.Setup > 0)
                builder.SetSetup(ligting.Project.Setup);

            if (ligting.Class > 0)
                builder.SetProductClass(ligting.Class);

            builder.SetSeries(ligting.Series);

            if (ligting.AdapterLighting != null && ligting.AdapterLighting.Class > 0)
                builder.SetAdpaterClass(ligting.AdapterLighting.Class);

            byte[] response = await TCPClient.Current.Call((int)CommandType.RelatedProduct, builder.Build().ToByteArray());
            if (response == null || response.Length == 0 || response[0] == 0)
            {
                MessageHelper.ShowError("加载数据出错。");
                return;
            }
            ObservableCollection<LightingViewModel> lightings = new ObservableCollection<LightingViewModel>();
            foreach (Lighting lighting in Lightings.ParseFrom(response).LightingList)
            {
                lightings.Add(ConvertRelateProductForm(lighting));
            }
            Pictures.ItemsSource = lightings;
        }

        private LightingViewModel ConvertRelateProductForm(Lighting lighting)
        {
            LightingViewModel vm = new LightingViewModel();
            if (lighting.HasSeries)
                vm.Series = lighting.Series;

            if (lighting.HasClass)
                vm.Class = lighting.Class;

            if (lighting.HasLable)
                vm.Lable = lighting.Lable;

            if (lighting.HasAdapterLighting && lighting.AdapterLighting.HasClass)
            {
                vm.AdapterLighting = new AdapterLightingVM();
                vm.AdapterLighting.Class = lighting.AdapterLighting.Class;
            }

            if (lighting.HasPicture && lighting.Picture.HasFacade)
            {
                vm.Picture = new PictureVM();
                vm.Picture.FacadeView = lighting.Picture.Facade;
            }

            return vm;
        }

        public PageLevel PageLevel
        {
            get { return PageLevel.Fourth; }
        }
    }
}
